$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/dict/item/list',
        datatype: "json",
        mtype:'POST',
        postData:{
          codeGuid : T.p('codeGuid')
        },
        colModel: [
            {label: '代码项文本', name: 'itemtext', index: 'itemtext', width: 80},
            {label: '代码项值', name: 'itemvalue', index: 'itemvalue', width: 80},
            {label: '排序号', name: 'ordernumber', index: 'ordernumber',editable: true,edittype: "text", editrules:{ number: true}, width:80},
            {label: '排序号', name: 'itemguid', index: 'itemguid', width:80,hidden:true},
            {label: '排序号', name: 'codeguid', index: 'codeguid', width:80,hidden:true}
        ],
        cellEdit:true,//是否开启单元格的编辑功能
        cellsubmit:'clientArray',//or 'clientArray',remote代表每次编辑提交后进行服务器保存，clientArray只保存到数据表格不保存到服务器
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        jsonReader: {
            root: "itemList",
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp1',
    data: {
        i: {
            itemtext: null,
            itemvalue: null,
            ordernumber:null
        },
        dict: {}
    },
    methods: {
        add: function () {

            if (!vm.i.itemtext) {
                alert("代码项文本必须填写！");
                return;
            }

            if (!vm.i.itemvalue) {
                alert("代码项值必须填写！");
                return;
            }

            vm.i.codeguid = T.p('codeGuid');

            $.ajax({
                type: "POST",
                url: baseURL + "sys/dict/item/save",
                contentType: "application/json",
                data: JSON.stringify(vm.i),
                success: function (r) {
                    if (r.code == 0) {
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                            vm.i.itemtext = null;
                            vm.i.itemvalue = null;
                            vm.i.ordernumber = null;
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var rowDatas = getSelectedRows();
            if (rowDatas == null) {
                return;
            }

            var ids = [];
            rowDatas.forEach(function (rowData) {
                ids.push(rowData.itemguid);
            })


            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/dict/item/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        update:function(){
            var rowDatas =getAllRows();
            $.ajax({
                type: "POST",
                url: baseURL + "sys/dict/item/update",
                contentType: "application/json",
                data: JSON.stringify(rowDatas),
                success: function (r) {
                    if (r.code == 0) {
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        }
    }
});